using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data;
using System.Data.Common;
using NCT.Framework.Database;

namespace VISS.API.Users {
	class UserReportDA {

		private const string SP_Delete = "dbo.UserReport_Delete";
        private const string SP_GetAllByStatus = "dbo.[UserReport_GetAllByStatus]";
		private const string SP_GetByReportedUserKey = "dbo.UserReport_GetByReportedUserKey";
		private const string SP_GetByReportedUserKeyAndStatus = "dbo.UserReport_GetByReportedUserKeyAndStatus";
        private const string SP_Insert = "dbo.UserReport_Insert";

		public static UserReportEnt CreateFromReader(IDataReader reader) {
			UserReportEnt item = new UserReportEnt();
			item.ID = ConvertUtils.ToLong(reader["ID"]);
			item.UserKey = ConvertUtils.ToString(reader["UserKey"]);
			item.ReportedUserKey = ConvertUtils.ToString(reader["ReportedUserKey"]);
			item.Reason = ConvertUtils.ToString(reader["Reason"]);
			item.Status = (EUserReportStatus)ConvertUtils.ToShort(reader["Status"]);
			item.DateCreated = ConvertUtils.ToDateTime(reader["DateCreated"]);
            item.ReporterName = ConvertUtils.ToString(reader["ReporterName"]);
            item.ReportedName = ConvertUtils.ToString(reader["ReportedName"]);
			return item;
		}

		public static int Delete(long iD, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Delete)) {
				db.AddParameter(cmd, "ID", iD);
                db.AddParameter(cmd, "Status", (short)EUserReportStatus.Deleted);
				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

        public static List<UserReportEnt> GetAllByStatus(EUserReportStatus status, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            List<UserReportEnt> result = new List<UserReportEnt>();
            Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_GetAllByStatus)) {
                db.AddParameter(cmd, "Status", (short)status);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    while (reader.Read()) {
                        result.Add(CreateFromReader(reader));
                    }
                }
            }
            return result;
        }

        public static List<UserReportEnt> GetByReportedUserKey(string reportedUserKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			List<UserReportEnt> result = new List<UserReportEnt>();
			Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_GetByReportedUserKey)) {
				db.AddParameter(cmd, "ReportedUserKey", reportedUserKey);
				using (IDataReader reader = db.ExecuteReader(cmd)) {
					while (reader.Read()) {
						result.Add(CreateFromReader(reader));
					}
				}
			}
			return result;
		}

        public static int Insert(UserReportEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Insert)) {
				db.AddParameterOuter(cmd, "ID", DbType.Int64);
				db.AddParameter(cmd, "UserKey", item.UserKey);
				db.AddParameter(cmd, "ReportedUserKey", item.ReportedUserKey);
				db.AddParameter(cmd, "Reason", item.Reason);
				db.AddParameter(cmd, "Status", item.Status);
				db.AddParameter(cmd, "DateCreated", item.DateCreated);

				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
					item.ID = ConvertUtils.ToLong(cmd.Parameters["ID"].Value);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
					item.ID = ConvertUtils.ToLong(cmd.Parameters["ID"].Value);
				}
			}
			return result;
		}

	}
}